<template>
  <div class="login df aic jcc">
    <div class="form df fdc aic jcc">
      <h1 class="loginText">系统登录</h1>
      <el-form :model="ruleForm" size="small" :rules="rules" ref="formRef">
        <!-- 用户名 -->
        <el-form-item prop="account">
          <el-input type="text" v-model="ruleForm.account">
            <i class="iconfont icon-user" slot="prefix"></i>
          </el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            :type="flag ? 'text' : 'password'"
            v-model="ruleForm.password"
            @keyup.13.native="submit"
          >
            <i slot="prefix" class="iconfont icon-password"></i>
            <i
              @click="flag = !flag"
              slot="suffix"
              class="iconfont icon-eye-close"
              :class="flag ? 'icon-eye-open' : 'icon-eye-close'"
            ></i>
          </el-input>
        </el-form-item>
        <!-- 登录按钮 -->
        <el-form-item>
          <el-button type="primary" @click="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
//解构vuex辅助函数
import {mapActions} from 'vuex'
export default {
  data() {
    return {
      // 表单
      ruleForm: {
        password: '',
        account: '',
      },
      // 眼睛
      flag: false,
      // 表单验证
      rules: {
        account: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur'},
        ],
      },
    }
  },
  // 提交验证
  methods: {
    ...mapActions('user', ['A_login']),
    //登录操作
    submit() {
      this.$refs.formRef.validate(async valid => {
        //判断
        if (valid) {
          //发送请求 等待Promise状态切换完成 执行后面代码
          await this.A_login(this.ruleForm)

          //跳转操作
          this.$router.push('/home')
        } else {
          console.log('验证失败')
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.login {
  background: $menuBg;
  height: 100%;
  .loginText {
    font-size: 25px;
    font-weight: 700;
    color: white;
    margin-bottom: 30px;
  }
}
.el-form,
.el-button {
  width: 300px;
}

.el-input__inner {
  background: #000 !important;
}
// 深度穿透
:deep(.el-input) {
  font-size: 15px;
}
:deep(.el-input__inner) {
  background-color: transparent;
  color: white;
}
</style>
